package com.ptszyxx.popose.module.main.dynamic.vm;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.ptszyxx.popose.R;
import com.ysg.http.data.CommonRepository;
import com.ptszyxx.popose.common.utils.YBusUtil;
import com.ysg.http.data.entity.dynamic.CommentEntity;
import com.ysg.http.data.entity.dynamic.ReplyResult;
import com.ysg.base.BaseViewModel;
import com.ysg.binding.command.BindingAction;
import com.ysg.binding.command.BindingCommand;
import com.ysg.bus.event.SingleLiveEvent;
import com.ysg.http.BaseResponse;
import com.ysg.http.HttpUtils;
import com.ysg.http.callback.OnSuccessResult;
import com.ysg.utils.YStringUtil;
import com.ysg.utils.YToastUtil;

import java.util.HashMap;
import java.util.Map;

public class DynamicReplyVM extends BaseViewModel<CommonRepository> {
    public int pageNo;
    public String commentId;
    public CommentEntity commentEntity;
    public ObservableField<String> input = new ObservableField<>();

    public DynamicReplyVM(@NonNull Application application, CommonRepository model) {
        super(application, model);
    }

    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        public SingleLiveEvent onRefreshEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onInputClearEvent = new SingleLiveEvent<>();
    }

    public BindingCommand onSendCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            requestSendComment();
        }
    });


    //=========================================接口：start===========================================
    /**
     * 回复列表
     */
    public void requestReplyPage(boolean refresh){
        if (refresh){
            pageNo = 1;
        }else{
            pageNo++;
        }
        Map<String, String> map = new HashMap<>();
        map.put("commentid", commentId);
        map.put("pageNo", String.valueOf(pageNo));
        HttpUtils.getInstance()
                .data(model.dynamicReplyPage(map), this,
                        new OnSuccessResult<ReplyResult>() {
                            @Override
                            public void onSuccessResult(BaseResponse<ReplyResult> response) {
                                uc.onRefreshEvent.setValue(response.getData().getList());
                            }
                        });
    }

    /**
     * 发送评论
     */
    public void requestSendComment(){
        if (YStringUtil.isEmpty(input.get())){
            YToastUtil.showShort(R.string.dynamic_detail_comment_hint);
            return;
        }
        Map<String, String> map = new HashMap<>();
        map.put("message", input.get());
        map.put("commentid", commentId);

        HttpUtils.getInstance()
                .data(model.dynamicCommentReply(map), this,
                        new OnSuccessResult() {
                            @Override
                            public void onSuccessResult(BaseResponse response) {
                                YToastUtil.showShort(R.string.success_comment);
                                requestReplyPage(true);
                                uc.onInputClearEvent.call();
                                YBusUtil.sendReplySuccess();
                            }
                        });
    }

}
